﻿using System;
using System.Data;
using System.Data.SQLite;
using System.Text;

namespace App.Bus
{
    /// <summary>
    /// 类Income。
    /// </summary>
    [Serializable]
    public partial class Income
    {
        public Income()
        { }
        #region Model
        private int _id;
        private DateTime _insertdatetime;
        private int _accountid;
        private int _savemoney;
        /// <summary>
        /// 
        /// </summary>
        public int Id
        {
            set { _id = value; }
            get { return _id; }
        }
        /// <summary>
        /// 
        /// </summary>
        public DateTime InsertDateTime
        {
            set { _insertdatetime = value; }
            get { return _insertdatetime; }
        }
        /// <summary>
        /// 
        /// </summary>
        public int AccountId
        {
            set { _accountid = value; }
            get { return _accountid; }
        }
        /// <summary>
        /// 
        /// </summary>
        public int SaveMoney
        {
            set { _savemoney = value; }
            get { return _savemoney; }
        }
        #endregion Model


        #region  Method

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Income(int Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,InsertDateTime,AccountId,SaveMoney ");
            strSql.Append(" FROM [Income] ");
            strSql.Append(" where Id=@Id ");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@Id", DbType.Int32,4)};
            parameters[0].Value = Id;

            DataSet ds = DbHelperSQLite.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"] != null && ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    this.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["InsertDateTime"] != null && ds.Tables[0].Rows[0]["InsertDateTime"].ToString() != "")
                {
                    this.InsertDateTime = DateTime.Parse(ds.Tables[0].Rows[0]["InsertDateTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["AccountId"] != null && ds.Tables[0].Rows[0]["AccountId"].ToString() != "")
                {
                    this.AccountId = int.Parse(ds.Tables[0].Rows[0]["AccountId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["SaveMoney"] != null && ds.Tables[0].Rows[0]["SaveMoney"].ToString() != "")
                {
                    this.SaveMoney = int.Parse(ds.Tables[0].Rows[0]["SaveMoney"].ToString());
                }
            }
        }

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQLite.GetMaxID("Id", "Income");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from [Income]");
            strSql.Append(" where Id=@Id ");

            SQLiteParameter[] parameters = {
					new SQLiteParameter("@Id", DbType.Int32,4)};
            parameters[0].Value = Id;

            return DbHelperSQLite.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into [Income] (");
            strSql.Append("InsertDateTime,AccountId,SaveMoney)");
            strSql.Append(" values (");
            strSql.Append("@InsertDateTime,@AccountId,@SaveMoney)");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@InsertDateTime", DbType.DateTime),
					new SQLiteParameter("@AccountId", DbType.Int32,8),
					new SQLiteParameter("@SaveMoney", DbType.Int32,8)};
            parameters[0].Value = InsertDateTime;
            parameters[1].Value = AccountId;
            parameters[2].Value = SaveMoney;

            DbHelperSQLite.ExecuteSql(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update [Income] set ");
            strSql.Append("InsertDateTime=@InsertDateTime,");
            strSql.Append("AccountId=@AccountId,");
            strSql.Append("SaveMoney=@SaveMoney");
            strSql.Append(" where Id=@Id ");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@InsertDateTime", DbType.DateTime),
					new SQLiteParameter("@AccountId", DbType.Int32,8),
					new SQLiteParameter("@SaveMoney", DbType.Int32,8),
					new SQLiteParameter("@Id", DbType.Int32,8)};
            parameters[0].Value = InsertDateTime;
            parameters[1].Value = AccountId;
            parameters[2].Value = SaveMoney;
            parameters[3].Value = Id;

            int rows = DbHelperSQLite.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from [Income] ");
            strSql.Append(" where Id=@Id ");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@Id", DbType.Int32,4)};
            parameters[0].Value = Id;

            int rows = DbHelperSQLite.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public void GetModel(int Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,InsertDateTime,AccountId,SaveMoney ");
            strSql.Append(" FROM [Income] ");
            strSql.Append(" where Id=@Id ");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@Id", DbType.Int32,4)};
            parameters[0].Value = Id;

            DataSet ds = DbHelperSQLite.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"] != null && ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    this.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["InsertDateTime"] != null && ds.Tables[0].Rows[0]["InsertDateTime"].ToString() != "")
                {
                    this.InsertDateTime = DateTime.Parse(ds.Tables[0].Rows[0]["InsertDateTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["AccountId"] != null && ds.Tables[0].Rows[0]["AccountId"].ToString() != "")
                {
                    this.AccountId = int.Parse(ds.Tables[0].Rows[0]["AccountId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["SaveMoney"] != null && ds.Tables[0].Rows[0]["SaveMoney"].ToString() != "")
                {
                    this.SaveMoney = int.Parse(ds.Tables[0].Rows[0]["SaveMoney"].ToString());
                }
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM [Income] ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQLite.Query(strSql.ToString());
        }

        #endregion  Method
    }
}

